package com.example.controller;

import com.example.common.CustomerException;
import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Admin;
import com.example.entity.User;
import com.example.service.AdminService;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
public class WebController {

    @Resource
    AdminService adminService;
    @Resource
    UserService userService;

    @GetMapping("/hello")
    public Result hello() {
        return Result.success("success");
    }

    @GetMapping("/erroruuuuu")
    public Result error(String name) {
        return Result.success(adminService.admin(name));
    }

    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account dbaccount=null;
        if("ADMIN".equals(account.getRole())){
            dbaccount=adminService.login(account);
        }else if("USER".equals(account.getRole())){
            dbaccount=userService.login(account);
        }else {
            throw new CustomerException(501,"非法用户");
        }
        return Result.success(dbaccount);
    }
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        userService.register(user);
        return Result.success("success");
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        Account dbaccount=null;
        if("ADMIN".equals(account.getRole())){
            adminService.updatePassword(account);
        }else if("USER".equals(account.getRole())){
            userService.updatePassword(account);
        }else {
            throw new CustomerException(501,"非法用户");
        }
        return Result.success("success");
    }

}
